package com.example.myapplication.utils;

import android.text.Editable;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import java.util.ArrayList;
import java.util.List;
/**
 * created by hzs 2021/09/23
 * 号码隔断工具，不使用EditText时，传入String隔断
 */
public abstract class BaseSplit {
    private final SparseArray<SparseArray<Character>> formatArray = new SparseArray<>();
    private final List<SplitNumberSpan> mSpanList = new ArrayList();
    private boolean mEnable = true;//隔断功能是否可用

    public BaseSplit() {
        initFormat();
    }

    /**
     * 初始化模板，在此函数里使用setFormat(String format)设置隔断模板
     */
    public abstract void initFormat();

    /**
     * 设置隔断格式
     *
     * @param format 隔断格式，号码用A代替，如输入“AAA AAA”表示输入六位号码时的隔断样式
     *               同一位数的号码只能使用一种隔断方式
     */
    public void setFormat(String format) {
        SparseArray<Character> wildcardArray = new SparseArray<>();
        int count = 0;
        for (int i = 0; i < format.length(); i++) {
            char c = format.charAt(i);
            if (c == 'A') {
                count++;
            } else {
                wildcardArray.append(i, c);
            }
        }
        formatArray.append(count, wildcardArray);
    }

    /**
     * 处理隔断
     *
     * @param charSequence 未隔断的原始号码
     * @param editable     隔断的号码
     */
    protected void handleSpan(CharSequence charSequence, Editable editable) {
        if (!mEnable) {
            return;
        }
        for (int i = 0; i < mSpanList.size(); i++) {//清空span，得到未隔断的原始号码
            editable.removeSpan(mSpanList.get(i));
        }
        SparseArray<Character> wildCardArray = formatArray.get(editable.length());
        if (wildCardArray == null) {
            return;
        }
        mSpanList.clear();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < wildCardArray.size(); i++) {//遍历分隔符
            int key = wildCardArray.keyAt(i);
            int nextKey = wildCardArray.keyAt(i + 1);
            if (nextKey == key + 1) {//判断是否是连续隔断符
                sb.append(wildCardArray.get(key));
            } else {
                int insertPosition = key - i;//插入分隔符的位置
                if (insertPosition > charSequence.length() - 1) {//处理号码右侧的分隔符
                    sb.insert(0, charSequence.charAt(charSequence.length() - 1));
                    sb.append(wildCardArray.get(key));
                } else {
                    sb.append(wildCardArray.get(key));
                    sb.append(charSequence.charAt(insertPosition));
                }
                SplitNumberSpan span = new SplitNumberSpan(sb.toString());
                mSpanList.add(span);
                int index = Math.min(insertPosition, charSequence.length() - 1);//下标取最小值，防止最右侧号码右侧隔断符越界
                editable.setSpan(span, index, index + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                sb = new StringBuilder();
            }
        }
        Log.i("handleSpan", "editable:" + editable);
    }

    /**
     * 不使用EditText时，获得隔断后的号码
     * @param originalNumber 要隔断的号码
     * @return 隔断后的号码
     */
    public String getFormatNumber(String originalNumber) {
        if (TextUtils.isEmpty(originalNumber)) {
            return "";
        }
        SparseArray<Character> wildCardArray = formatArray.get(originalNumber.length());
        if (wildCardArray == null) {
            return originalNumber;
        }
        StringBuilder sb = new StringBuilder(originalNumber);
        for (int i = 0; i < wildCardArray.size(); i++) {//遍历分隔符
            int key = wildCardArray.keyAt(i);
            sb.insert(key, wildCardArray.get(key));
        }
        return sb.toString();
    }

    public boolean isEnable() {
        return mEnable;
    }

    public void setEnable(boolean mEnable) {
        this.mEnable = mEnable;
    }
}
